package com.zzy.controller;

import com.zzy.utils.ElectUrl;
import com.zzy.utils.ListUrl;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletContext;
import java.util.List;

@RestController
public class MyController2
{
    @RequestMapping("/bb")
    public String bb() throws Exception
    {
        String randomUrl = ElectUrl.getRandomUrl();
        if(randomUrl!=null)
        {
            CloseableHttpClient client = HttpClients.createDefault();
            HttpGet httpGet = new HttpGet(randomUrl+"/aa");
            CloseableHttpResponse resp = client.execute(httpGet);
            HttpEntity entity = resp.getEntity();
            if(entity!=null)
            {
                //遍历子节点,能访问就结束
                return "从服务器:"+randomUrl+"拿到了接口返回数据"+ EntityUtils.toString(entity);
            }
        }
        return "出问题了,没有可用的服务器";
    }

}
